const COLLECTION_STATUS = {
  WISH: {
    id: 1,
    key: 'wish',
    text: '想看'
  },
  COLLECT: {
    id: 2,
    key: 'collect',
    text: '看过'
  },
  DOING: {
    id: 3,
    key: 'doing',
    text: '在看'
  },
  ON_HOLD: {
    id: 4,
    key: 'on_hold',
    text: '搁置'
  },
  DROPPED: {
    id: 5,
    key: 'dropped',
    text: '弃置'
  }
};

const COLLECTION_COLUMNS = Object.values(COLLECTION_STATUS).map(status => ({
  key: status.key,
  text: status.text
}));
const COLLECTION_MAP = (() => {
  const map = {};
  Object.values(COLLECTION_STATUS).forEach(status => {
    map[status.key] = status.id;
    map[status.id] = status.key;
  });
  return map;
})();

const RATE_MAP = {
  1: '不忍直视',
  2: '很差',
  3: '差',
  4: '较差',
  5: '无过无失',
  6: '还行',
  7: '推荐',
  8: '力荐',
  9: '神作',
  10: '超神作'
};

function isValidCollection(value) {
  return value in COLLECTION_MAP;
}

function getCollectionStatus(value) {
  if (!isValidCollection(value)) return COLLECTION_STATUS.WISH;
  
  const key = typeof value === 'number' ? COLLECTION_MAP[value] : value;
  return COLLECTION_STATUS[key.toUpperCase().replace('-', '_')] || COLLECTION_STATUS.WISH;
}

Component({
  properties: {
    nameCn: {
      type: String,
      value: '默认中文名称'
    },
    name: {
      type: String,
      value: 'デフォルトの日本語名'
    },
    ratingScore: {
      type: Number,
      value: 8
    },
    roast: {
      type: String,
      value: ''
    },
    collection: {
      type: null,
      value: 'wish'
    },
    isPrivate: {
      type: Boolean,
      value: true
    }
  },

  observers: {
    'nameCn, name'(nameCn, name) {
      if (!nameCn || nameCn === '默认中文名称') {
        this.setData({
          nameCn: name
        });
      }
    }
  },

  data: {
    collectionColumns: COLLECTION_COLUMNS,
    currentCollection: null,
    checked: true,
    checkedText: '私密',
    currentRate: 0,
    currentRoast: '',
    rateDescription: '未评分',
    _timer: null
  },

  lifetimes: {
    attached() {
      const collectionStatus = getCollectionStatus(this.properties.collection);

      this.setData({
        currentCollection: collectionStatus,
        activeCollectionIndex: collectionStatus.id - 1,
        checked: this.properties.isPrivate,
        checkedText: this.properties.isPrivate ? '私密' : '公开',
        currentRate: this.properties.ratingScore,
        showRatingText: this.properties.ratingScore > 0,
        currentRoast: this.properties.roast,
        rateDescription: this.getRateDescription(this.properties.ratingScore)
      });
    }
  },

  methods: {
    getRateDescription(rate) {
      const roundedRate = Math.round(rate);
      return RATE_MAP[roundedRate] || '未评分';
    },

    onRateChange({ detail }) {
      if (this.data._timer) clearTimeout(this.data._timer)

      this.data._timer = setTimeout(() => {
        const rate = detail * 2;
        this.setData({
          currentRate: rate,
          rateDescription: this.getRateDescription(rate),
          showRatingText: rate > 0
        });
      }, 100)
    },
    onClearRate() {
      this.setData({
        currentRate: 0,
        rateDescription: '未评分',
        showRatingText: false
      });
    },

    onRoastChange({ detail }) {
      this.setData({
        currentRoast: detail
      });
    },

    onCollectionChange({ detail }) {
      const newStatus = getCollectionStatus(COLLECTION_COLUMNS[detail.index].key);
      this.setData({
        currentCollection: newStatus,
        activeIndex: detail.index
      });
    },
    
    onPrivacyChange({ detail }) {
      this.setData({ 
        checked: detail,
        checkedText: detail ? "私密" : "公开"
      });
    },
    
    onSubmit() {
      const formData = {
        nameCn: this.properties.nameCn,
        name: this.properties.name,
        ratingScore: this.data.currentRate,
        roast: this.data.currentRoast,
        collection: {
          number: this.data.currentCollection.id,
          string: this.data.currentCollection.key
        },
        isPrivate: this.data.checked
      };

      this.triggerEvent('submit', { formData });
    }
  }
});